package hejie.admin.dao.impl;

import java.sql.SQLException;
import java.util.List;

import javax.annotation.Resource;

import hejie.admin.dao.EthnicDao;

import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.springframework.orm.hibernate3.HibernateCallback;
import org.springframework.orm.hibernate3.HibernateTemplate;
import org.springframework.stereotype.Component; 

import publics.model.Ethnic;

/**
 * @author  hejie
 * @version  1.0
 *  
 * Dao层接口实现
 */
//注入
@Component("ethnicDao")
public class EthnicDaoImpl implements EthnicDao{
	HibernateTemplate hibernateTemplate;// 注入hibernate的spring代理模版

	public HibernateTemplate getHibernateTemplate() {
		return hibernateTemplate;
	}

	// 注入 hibernate模版
	@Resource(name = "hibernateTemplate")	
	public void setHibernateTemplate(HibernateTemplate hibernateTemplate) {
		this.hibernateTemplate = hibernateTemplate;
		System.out.println("hibernateTemplate injected in  EthnicDaoImpl.");
		// 测试用，实际运行中可去掉这句
	}	
	
	public Ethnic findById(Integer id){		
		return this.getHibernateTemplate().get(Ethnic.class, id);
	}
	
   @SuppressWarnings("unchecked")
   public List<Ethnic> findByPage(final String hql, final int offset, final int pageSize) {
		List<Ethnic> list = this.getHibernateTemplate().executeFind(
				new HibernateCallback() {
					public Object doInHibernate(Session session) throws HibernateException ,SQLException {
						//执行hibernate分页查询
						List<Ethnic> result = session.createQuery(hql)
							.setFirstResult(offset)
							.setMaxResults(pageSize)
							.list();
						return result;
					}
				}
				);
		
		return list;
	}
   
   public int findByCount(String sql) {	
		return this.getHibernateTemplate().find(sql).size();		 
   }	 
}
